﻿package com.dg.gaming.api.common.util {
    import com.ericfeminella.collections.*;

    import flash.utils.ByteArray;
    import flash.utils.Endian;

    /**
     * ...
     * @author ...
     */
    public class DGUtils {

        public function DGUtils() {

        }

        /**
         * Little helper method which reads as a String and formats it using a
         * specified format String.
         *
         * @param dateString      Date as a String
         * @param formatString    Format of the String
         * @return                Date object.
         * @see java.text.SimpleDateFormat
         */
        public static function readDate(dateString:String, formatString:String):Date {
            var tempDate:Date = null;
            try {
                //var dateFormatter:SimpleDateFormat = new SimpleDateFormat(formatString);
                tempDate = new Date;

            } catch (parseException:Exception) {
                // Simply return null.
            }
            return tempDate;
        }

        /**
         * Return the data as a String formatted with a specified String.
         *
         * @param date          Specified data / time.
         * @param formatString  Format of string.
         * @return              String representation of date.
         */
        public static function valueOf(date:Date, formatString:String):String {

            return formatString;
        }

        /**
         * Convert simple integer array to space delimited String.
         *
         * @param values  Array of integers.
         * @return        String value.
         */
        public static function valueOfArray(values:Array):String {
            var sb:StringBuffer = new StringBuffer;
            for (var i:int = 0; i < values.length; i++) {
                sb.append(values[i]);
                if (i < values.length - 1) {
                    sb.append(" ");
                }
            }

            return sb.toString();
        }

        /**
         * Convert String to int array.
         *
         * @param text  Simple integer array as a spaced delimited String.
         */
        public static function convertToIntArray(text:String):Array {
            var st:StringTokenizer = new StringTokenizer(text, " ");
            var values:Array = new Array();
            var len:int = st.countTokens();
            // Populate line info array
            for (var i:int = 0; i < len; i++) {
                values[i] = st.nextToken();
            }

            return values;
        }

        /**
         * Return a Color object from a String of "r,g,b" or "r,g,b,a"
         *
         * @param colourStr  Colour as a comma delimited String.
         * @return           Return a Color object.
         */
        public static function getColour(colourStr:String):Color {
            try {
                var st:StringTokenizer = new StringTokenizer(colourStr, ",");
                var r:int = parseInt(st.nextToken());
                var g:int = parseInt(st.nextToken());
                var b:int = parseInt(st.nextToken());
                if (st.hasMoreTokens()) {
                    var a:int = parseInt(st.nextToken());
                    return new Color(r, g, b, a);
                } else {
                    return new Color(r, g, b);
                }
            } catch (e:Exception) {
                trace("Error converting colour from " + colourStr);
                return null;
            }
            return null;
        }

        /**
         * Return a new Color from an existing color by add each of its
         * rgb values with an integer delta amount.  E.g. If a color
         * rgb (100, 100, 100), delta = 10 => New Color (110, 110, 110).
         *
         * @param color   Original Colour.
         * @param delta   Amount to add color to each channel.
         * @return        New Color
         */
        public static function getColorDelta(color:Color, delta:int):Color {
            var r:int = color.getRed() + delta < 256 ? color.getRed() + delta : 255;
            var g:int = color.getGreen() + delta < 256 ? color.getGreen() + delta : 255;
            var b:int = color.getBlue() + delta < 256 ? color.getBlue() + delta : 255;
            return new Color(r, g, b);
        }

        /**
         * Helper method for converting a 2 dimensional array to a single
         * array.
         *
         * @param array  Single dimension array of data.
         * @param l1     Length 1.
         * @param l2     Length 2
         * @return       Returns a 2d array of size [l1][l2] of type
         */
        public static function convertTo2DArray(array:Array, l1:int, l2:int):Array {
            var doubleArray:Array = new Array();

            var c:int = 0;
            for (var i:int = 0; i < l1; i++) {
                doubleArray[i] = new Array();
                for (var j:int = 0; j < l2; j++) {
                    doubleArray[i][j] = array[c++];
                }
            }

            return doubleArray;
        }

        /**
         * Helper method for converting a 2 dimensional array to a single
         * array.
         *
         * @param array   2 dimensional array.
         * @return        1 dimensional array with contents of 2d array.
         */
        public static function convertTo1DArray(array:Array):Array {
            var l1:int = array.length;
            var l2:int = array[0].length;
            var singleArray:Array = new Array();
            var c:int = 0;
            for (var i:int = 0; i < l1; i++) {
                for (var j:int = 0; j < l2; j++) {
                    singleArray[c++] = array[i][j];
                }
            }

            return singleArray;
        }

        public static function enscriptBytes(st:String):ByteArray {
            var rawData:ByteArray = new ByteArray();
            rawData.writeUTFBytes(st);
            rawData.compress();
            return rawData;
        }


        public static function descriptBytes(bt:ByteArray):String {
            var str:String = bt.toString();
            var rawData:ByteArray = new ByteArray();
            rawData.writeObject(bt);
//            rawData.endian = Endian.BIG_ENDIAN;
//            rawData.position = 0;
            bt.uncompress();
//            var enc:GZIPBytesEncoder = new GZIPBytesEncoder();
//            var uncBt:ByteArray = enc.uncompressToByteArray(rawData);
            return bt.toString();
        }
    }

}